var Channels=function(){
	var toolbar;
	var grid;
	var fs,win;

	var buildToolbar = function(){
		toolbar = new Ext.Panel({
			border:false,
			tbar:[{
				text:'新增',
				cls:'x-btn-text-icon',
				icon:RES_ROOT_PATH+'images/add.gif',
				handler:function(){
					win.setTitle('添加新通道');
					win.show();
					fs.form.reset();
					fs.url = './?ctrl=channels&action=create';
				}
			},'-',{
				text:'修改',
				cls:'x-btn-text-icon',
				icon:RES_ROOT_PATH+'images/edit.gif',
				handler:function(){
					var rec = grid.getSelectionModel().getSelected();
					if (rec){
						win.setTitle('修改通道信息');
						win.show();
						fs.form.findField('channel[gateway]').setValue(rec.data.gateway);
						fs.form.findField('channel[price]').setValue(rec.data.price);
						fs.form.findField('channel[account]').setValue(rec.data.account);
						fs.form.findField('channel[deduct]').setValue(rec.data.deduct);
						fs.form.findField('channel[decost]').setValue(rec.data.decost);
						fs.form.findField('channel[requestfile]').setValue(rec.data.requestfile);
						fs.form.findField('channel[responsefile]').setValue(rec.data.responsefile);
						fs.form.findField('channel[ptypes]').setValue(rec.data.ptypes);
						fs.id = rec.data.id;
						fs.url = './?ctrl=channels&action=update';
					}
					else{
						Ext.MessageBox.alert('警告','請選擇一個數據。');
					}
				}
			},'-',{
				text:'刪除',
				cls:'x-btn-text-icon',
				icon:RES_ROOT_PATH+'images/delete.gif',
				handler:function(){
					var rec = grid.getSelectionModel().getSelected();
					if (rec){
						Ext.Msg.confirm('提示', '是否确认删除？', function(btn){
							if (btn == 'yes'){
								Ext.Ajax.request({
									url:'./?ctrl=channels&action=delete',
									params:{
										id:rec.data.id
									},
									method:'POST',
									success:function(response){
										grid.store.reload();
									}
								});
							}
						},this);
					}else{
						Ext.MessageBox.alert('警告','請選擇一個數據。');
					}
				}
			}]
		});
	};

	var buildGrid = function(){
		var gatewayrend=function(value ,metadata ,record ,rowIndex ,colIndex ,store ){
			return gatewayData[value];
		};
		var accountrend=function(value ,metadata ,record ,rowIndex ,colIndex ,store ){
			return accontData[value];
		};
		var ptypesrenderer=function(value ,metadata ,record ,rowIndex ,colIndex ,store ){
			var arrays=value.split(',');
			for(var i=0;i<arrays.length;i++){
				arrays[i]=ptypeData[arrays[i]];
				
			}
			return arrays.join(',');
		};
		grid = new Ext.grid.GridPanel({
			store:new Ext.data.Store({
				proxy:new Ext.data.HttpProxy({url:'./?ctrl=channels&action=getAll'}),
				reader:new Ext.data.JsonReader({},[
				'id','gateway','price','account','deduct','decost','requestfile','responsefile','ptypes'
				]),
				autoLoad:true
			}),
			columns:[
			new Ext.grid.RowNumberer({header :'ID',dataIndex:'id'}),
			{header:'網管類型',width:80,sortable:true,dataIndex:'gateway',renderer: gatewayrend},
			{header:'單價',width:80,sortable:true,dataIndex:'price'},
			{header:'廣告類型',width:150,sortable:true,dataIndex:'ptypes',renderer:ptypesrenderer},
			{header:'結算方式',width:150,sortable:true,dataIndex:'account',renderer: accountrend},
			{header:'扣量方式',width:80,sortable:true,dataIndex:'deduct'},
			{header:'扣费类型',width:150,sortable:true,dataIndex:'decost'},
			{header:'提交文件',width:250,sortable:true,dataIndex:'requestfile'},
			{header:'接收文件',width:250,sortable:true,dataIndex:'responsefile'}
			
			],
			renderTo:'grid',
			border:false
		});
	};

	var buildLayout = function(){
		new Ext.Viewport({
			layout:'border',
			defaults:{border:false},
			items:[{
				region:'north',
				height:50,
				title:'通道管理',
				items:toolbar
			},{
				region:'center',
				layout:'fit',
				items:grid
			}]
		});
	};

	var buildForm = function(){
		var gatewaycomb=new Ext.form.ComboBox({
			fieldLabel: '網管類型',
			//hiddenName:'channel[gateway]',
			transform:'channel[gateway]',
			mode:'local',
			lazyRender:true,
			listClass: 'x-combo-list-small',
			typeAhead: true,
			readOnly :true,
			triggerAction: 'all',
			selectOnFocus:true,
			allowBlank:false,
			width:"50%"
		});
		var accountcomb=new Ext.form.ComboBox({
			fieldLabel: '結算方式',
			//hiddenName:'channel[account]',
			transform:'channel[account]',
			mode:'local',
			lazyRender:true,
			listClass: 'x-combo-list-small',
			typeAhead: true,
			readOnly :true,
			triggerAction: 'all',
			selectOnFocus:true,
			allowBlank:false,
			width:"50%"
		});
		var ptypelistsel = new Ext.ux.Multiselect({
			name:"channel[ptypes]",
			fieldLabel:  '廣告類型',
			dataFields:  ['id', 'title'], 
			data:  ptypeList,
			valueField:  'id',
			displayField:  'title',
			width:  150,
			height:  150,
			allowBlank:true
		});
		fs = new Ext.form.FormPanel({
			labelWidth:90,
			labelAlign:'right',
			defaultType:'textfield',
			defaults:{width:200},
			bodyStyle:'padding:10;',
			frame:true,
			items:[ptypelistsel,gatewaycomb,{
				fieldLabel:'單價',
				allowBlank:false,
				name:'channel[price]'
			},accountcomb,{
				fieldLabel:'扣量方式',
				allowBlank:false,
				name:'channel[deduct]'
			},{
				fieldLabel:'扣费类型',
				allowBlank:false,
				name:'channel[decost]'
			},{
				fieldLabel:'提交文件',
				allowBlank:false,
				name:'channel[requestfile]'
			},{
				fieldLabel:'接收文件',
				allowBlank:false,
				name:'channel[responsefile]'
			}]
		});
	};

	var buildWin = function(){
		win = new Ext.Window({
			width:400,
			height:460,
			layout:'fit',
			closeAction:'hide',
			modal:true,
			items:fs,
			buttons:[{
				text:'保存',
				handler:saveChannel
			},{
				text:'取消',
				handler:function(){win.hide();}
			}]
		});
	};

	var saveChannel = function(){
		fs.form.submit({
			waitMsg:'正在保存。。。',
			url:fs.url,
			params:{id:fs.id},
			method:'POST',
			success:function(){
				win.hide();
				grid.store.reload();
			}
		});
	};
	return{
		init:function(){
			buildToolbar();
			buildGrid();
			buildLayout();
			buildForm();
			buildWin();
		}
	}
}();
Ext.onReady(Channels.init,Channels);
